package com.donleo.ssm.controller;

import com.donleo.ssm.dto.UserDto;
import com.donleo.ssm.model.CodeModel;
import com.donleo.ssm.model.UserModel;
import com.donleo.ssm.service.ICodeService;
import com.donleo.ssm.service.IUserService;
import com.donleo.ssm.utils.CommonPath;
import com.donleo.ssm.utils.ResponseLayResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Integer.parseInt;

/**
 * @author liangd
 * date 2020-11-17 16:41
 * code
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 模糊查询
     * <p>
     * 用LayUI请求，默认不用添加@RequestBody
     */
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public ResponseLayResult findAll(UserDto userDto) {
        List<UserModel> userList = userService.findAll(userDto);
        Integer count = userService.getCount(userDto);
        return ResponseLayResult.createBySuccess(count, userList);
    }

    /**
     * 查询单个
     * RequestParam  ?id= 参数传值
     * PathVariable  /id= 路径传值
     *
     * @return
     * @requestParam 和@param的区别
     * 1、@requestParam 是Spring中的注解 一般用于Controller层
     * 2、@param 是mybatis中的注解 一般用于dao层
     * 两者的作用都是使参数的变量名保持一致
     */
    @GetMapping("/findById")
    public ResponseLayResult findById(Integer id) {
        UserModel userModel = userService.findById(id);
        return ResponseLayResult.createBySuccess(1, userModel);
    }

    /**
     * 添加
     *
     * @param userModel
     * @return
     */
    @PostMapping("/add")
    public ResponseLayResult add(@RequestBody UserModel userModel) {
        UserModel user = null;
        try {
            userModel.setId(null);
            user = userService.add(userModel);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        //将添加的Id返回给前端
        return ResponseLayResult.createBySuccess(1, user.getId());
    }

    /**
     * 单个删除
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public ResponseLayResult delete(Integer id) {
        Integer count = userService.delete(id);
        return ResponseLayResult.createBySuccess(count, count);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/deletes")
    public ResponseLayResult deletes(Integer[] ids) {
        List<Integer> idsList = Arrays.asList(ids);
        Integer count = userService.deletes(idsList);
        return ResponseLayResult.createBySuccess(count, count);
    }

    /**
     * 修改
     *
     * @param userModel
     * @return
     */
    @PostMapping("/update")
    public ResponseLayResult update(@RequestBody UserModel userModel) {
        //Assert断言---判断method中参数是否是正常值
        Assert.notNull(userModel.getId(), "Id is not null!");
        Integer count = userService.update(userModel);
        return ResponseLayResult.createBySuccess(count, count);
    }

    /**
     * 用户注册
     *
     * @param userModel
     * @return
     */
    @PostMapping("/register")
    public ResponseLayResult register(@RequestBody UserModel userModel) {
        Integer code = userService.register(userModel);
        return ResponseLayResult.createBySuccess(code, code);
    }

    /**
     * 用户登录
     *
     * @param userModel
     * @return
     */
    @PostMapping("/login")
    public ResponseLayResult login(@RequestBody UserModel userModel) {
        Integer code = userService.login(userModel);
        Integer userId = null;
        if (code == 200) {
            userId = userService.findUser(userModel);
        }
        return ResponseLayResult.createBySuccess(code, userId);
    }

    /**
     * 获取当前登录用户
     * @return
     */
    @GetMapping("/getLoginUser")
    public ResponseLayResult getLoginUser(){
        Jedis jedis = new Jedis();
        jedis.auth(CommonPath.REDIS_AUTH.getValue());
        String userIdStr = jedis.get("userId");
        return ResponseLayResult.createBySuccess(1,parseInt(userIdStr));
    }
}
